

import Decimal from 'decimal.js';

const TokenKey = 'ucen_template_token'


export function datetimeToString(date) {
  var y = date.getFullYear(); 
  var m = date.getMonth() + 1;  
      m = m < 10 ? ('0' + m) : m;  
  var d = date.getDate();  
      d = d < 10 ? ('0' + d) : d;  
  var h = date.getHours();  
      h=h < 10 ? ('0' + h) : h;  
  var minute = date.getMinutes();  
      minute = minute < 10 ? ('0' + minute) : minute;  
  var second=date.getSeconds();  
      second=second < 10 ? ('0' + second) : second;  
  return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; 
}

export function dateToString(date, type) {
    var y = date.getFullYear(); 
    var m = date.getMonth() + 1;  
        m = m < 10 ? ('0' + m) : m;  
    var d = date.getDate();  
        d = d < 10 ? ('0' + d) : d;
    if(type == 1) {
        return y + '/' + m + '/' + d; 
    }
    return y + '-' + m + '-' + d; 
}

export function stringToDate(strDate) {
  return new Date(strDate)
}

export function addDate(date, type, num) {
    var tmpDate = new Date(date)
    if(type == 'year') { 
        tmpDate.setFullYear(new Decimal(tmpDate.getFullYear()).plus(new Decimal(num)))
    } else if(type == 'month') {
        tmpDate.setMonth(new Decimal(tmpDate.getMonth()).plus(new Decimal(num)))
    } else if(type == 'day') {
        tmpDate.setDate(new Decimal(tmpDate.getDate()).plus(new Decimal(num)))
    } else if(type == 'hour') {
        tmpDate.setHour(new Decimal(tmpDate.getHour()).plus(new Decimal(num)))
    } else if(type == 'minute') {
        tmpDate.setMinute(new Decimal(tmpDate.getMinute()).plus(new Decimal(num)))
    } else if(type == 'second') {
        tmpDate.setSecond(new Decimal(tmpDate.getSecond()).plus(new Decimal(num)))
    }
    return tmpDate
}


export function subDate(date, type, num) {
    var tmpDate = new Date(date)
    if(type == 'year') {
        tmpDate.setFullYear(new Decimal(tmpDate.getFullYear()).sub(new Decimal(num)))
    } else if(type == 'month') {
        tmpDate.setMonth(new Decimal(tmpDate.getMonth()).sub(new Decimal(num)))
    } else if(type == 'day') {
        tmpDate.setDate(new Decimal(tmpDate.getDate()).sub(new Decimal(num)))
    } else if(type == 'hour') {
        tmpDate.setHour(new Decimal(tmpDate.getHour()).sub(new Decimal(num)))
    } else if(type == 'minute') {
        tmpDate.setMinute(new Decimal(tmpDate.getMinute()).sub(new Decimal(num)))
    } else if(type == 'second') {
        tmpDate.setSecond(new Decimal(tmpDate.getSecond()).sub(new Decimal(num)))
    } 
    return tmpDate
}

export function getDateTime(hour, minute, second) {
    var today = new Date()
    today.setHours(hour)
    today.setMinutes(minute)
    today.setSeconds(second)
    return today
}

export function betweenDate(date1, date2, type) {
    let one = 1
    if(type == 'second') {
        one = 1000; // 1秒钟毫秒数
    } else if(type == 'minute') {
        one = 60 * 1000; // 1分钟毫秒数
    } else if(type == 'hour') {
        one = 60 * 60 * 1000; // 1小时毫秒数
    } else if(type == 'day') {
        one = 24 * 60 * 60 * 1000; // 1天毫秒数
    } else if(type == 'month') {
        one = 30 * 24 * 60 * 60 * 1000; // TODO 月份30天？？
    } if(type == 'year') { 
        one = 12 * 30 * 24 * 60 * 60 * 1000; // TODO 月份30天？？
    } 
    
    const time1 = date1.getTime(); // 转换为毫秒数
    const time2 = date2.getTime(); // 转换为毫秒数
    const diff = Math.abs(time1 - time2); // 计算差异的绝对值
    return Math.floor(diff / one); // 将差异转换为天数并返回
}